import os
import zipfile
import random
import numpy as np
from PIL import Image

import paddle
from paddle.io import Dataset, DataLoader
from paddle import nn
from paddle.nn import functional as F

BATCH_SIZE = 32

src_path = "data/Gestures.zip"
dest_path = "data/"
data_path = dest_path + "/Main"

#解压文件
if not os.path.exists(data_path):
    z = zipfile.ZipFile(src_path, "r")
    z.extractall(dest_path)
    z.close()

train_list, test_list = [],[]

file_folders = os.listdir(data_path)

for floder in file_folders:
    imgs = os.listdir(os.path.join(data_path, floder))
    for index, img in enumerate(imgs, 1):

        img_path = os.path.join(data_path, floder, img)

        if index % 10 == 0:
            test_list.append([img_path, floder])
        else:
            train_list.append([img_path, floder])

#实现paddle固定的数据集API
class GestureDataSet(Dataset):
    #初始化函数
    def __int__(self, data_list, transform):
        super(GestureDataSet, self).__init__()
        self.data_list = data_list
        self.transform = transform

    def __getitem__(self, index):
        img_path, label = self.data_list[index]
        img = self.transform(img_path)
        return img , int(label)
    def __len__(self):
        return len(self.data_list)

def img_transform():
    img = Image.open(img_path)
    img.show()
    img = img.resize((224, 224), Image.ANTIALIAS)
    img.show()
    img = np.array(img).astype("float32")
    #调整通道数C的位置
    img = img.transpose((2, 0, 1))
    #放大缩小
    img = paddle.to_tensor(img/255)
    return img

train_dataset = GestureDataSet(train_list, img_transform)
test_dataset = GestureDataSet(test_list, img_transform)

train_loader = DataLoader(train_dataset,
                          batch_size=BATCH_SIZE,
                          num_workers=0,
                          shuffle=True)
test_dataset = DataLoader(test_dataset,
                          batch_size=BATCH_SIZE,
                          num_workers=0,
                          shuffle=True)

class AlexNet(nn.layer):
    #设置层
    def __int__(self, input_channels=3, reslut_classes=10):
        super(AlexNet, self).__init()
        self.conv1 = nn.Conv2D(input_channels, 96, 11, stride=4, padding=2)
        self.pool1 = nn.MaxPool2D(kernel_size=3, stride=2)
        self.conv2 = nn.Conv2D(96, 256, 5, stride=1, padding=2)
        self.pool2 = nn.MaxPool2D(3, 2)
        self.conv3 = nn.Conv2D(256, 384, 3, stride=1, padding=1)
        self.conv4 = nn.Conv2D(256, 384, 3, stride=1, padding=1)
        self.conv5 = nn.Conv2D(256, 384, 3, stride=1, padding=1)
        self.pool3 = nn.MaxPool2D(3, 2)
        self.fc1 = nn.Linear(6*6*256, 4096)
        self.drop1 = nn.Dropout(0.25)
        self.fc2 = nn.Linear(4096, 4096)
        self.drop1 = nn.Dropout(0.25)
        self.fc3 = nn.Linear(4096, 10)

    #设置层的连接方式
    def forward(self, x):
        x = F.relu(self.conv1)
        x = self.pool1(x)
        x = F.relu(self.conv2)
        x = self.pool2(x)
        x = F.relu(self.conv3)
        x = F.relu(self.conv4)
        x = F.relu(self.conv5)
        x = self.pool3(x)
        x = paddle.flatten(x, 1, -1)
        x = F.relu(self.fc1(x))
        x = self.drop1(x)
        x = F.relu()



